package org.vectrics.cms;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;

import org.apache.ecs.html.A;
import org.apache.ecs.html.IMG;
import org.apache.ecs.html.TD;
import org.apache.ecs.html.TR;
import org.apache.ecs.html.Table;
import org.vectrics.SystemException;
import org.vectrics.common.util.StringUtils;
import org.vectrics.domain.Domain;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.VectricsTagSupport;

public class ContentImageTag extends VectricsTagSupport {

	private static final long serialVersionUID = -2960013419580144960L;
	
	public static String TRUE = "true";
	public static String FALSE = "false";
	private String section = null;
	private String type = "text";
	private String code = null;
	private String domainId = null;
	private String alt = null;

	
	public String getDomainId() {
		return domainId;
	}

	public void setDomainId(String domainId) {
		this.domainId = domainId;
	}
	
	public Domain getDomain() {
		Domain domain = null;
		String id = this.getDomainId();
		if (id.startsWith("3110")) {
			id = id.substring(4);
		}
		if (StringUtils.isNullOrBlank(this.getDomainId())) {
			domain = CoreServiceLocator.getSystemService().findSystemDomain();
		} else {
			domain = CoreServiceLocator.getSystemService().findDomain(new Long(id));
		}
		if (domain == null) {
			throw new SystemException("Could not find domain for id: " + id);
		}
		return(domain);
	}

	/**
	 * Create tag content.
	 */
	protected String makeStartTag() {
		boolean editMode = false;
		int borderWidth = 0;
		StringBuffer html = new StringBuffer();
		
		ContentHelper contentHelper = new ContentHelper();
		String filePath = contentHelper.getContentFileName(getDomain(), this.getSection(), this.getCode());
		URL url = null;
		URLConnection urlConnection = null;
		try {
			url = new URL(filePath);
			urlConnection = url.openConnection();
		} catch (MalformedURLException ex) {
			log.error("Mal Formada la URL: " + filePath, ex);
			throw new SystemException("Mal Formada la URL: " + filePath, ex);
		} catch (IOException ex) {
			log.error("Exception opening URL: " + filePath, ex);
			throw new SystemException("Exception opening file: " + filePath, ex);
		}
		if (this.getEdit()) {
			editMode = true;
		}
		

		Table table = new Table();
		table.setCellPadding(0);
		table.setCellSpacing(0);
		table.setWidth("1%");
		table.setBorder(0);
		
		if (editMode) {
			borderWidth = 1;
			table.setBorder(1);
			TR row = new TR();
			table.addElement(row);
			TD cell = new TD();
			row.addElement(cell);
			cell.setBgColor("#ffbbbb");
			
			A link = new A();
            link.setOnClick("OpenLarge(this.href, '100'); return false");
			link.setHref("/hrs-intranet/admin/content/editimage.do?section=" +
					this.getSection() +
					"&code=" +	this.getCode()
				+ "&type=" +	this.getType());
			link.setClass("subLink");
			link.addElement("Change Image");
			cell.addElement(link);
		}

		TR contentRow = new TR();
		table.addElement(contentRow);
		TD contentCell = new TD();
		contentRow.addElement(contentCell);
		
		IMG image = null;
		
		image = new IMG();
		String host = this.getRequest().getContextPath();
		Date now = new Date();
		long uid = now.getTime();
		uid += this.getCode().hashCode();
		String src = host + "/cmsimage/" + this.getCode() + "." + this.getType()
			+ "?domain=" + this.getDomainId() + "&instance=" + uid
			+ "&section=" + this.getSection() + "&code=" + this.getCode();
		image.setSrc(src);
		image.setBorder(borderWidth);
		if (this.getAlt() != null)
			image.setAlt(this.getAlt());
		contentCell.addElement(image);
		
		
		if (editMode)
			html.append(table.toString());
		else
			html.append(image.toString());
	 
		return(html.toString());
	}
	

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getSection() {
		return section;
	}

	public void setSection(String section) {
		this.section = section;
	}

	public boolean getEdit() {
		boolean edit = false;
		Boolean editContent = (Boolean)this.getRequest().getSessionAttribute("editContent");
		
		if (editContent != null) {
			edit = editContent.booleanValue();
		}
		return(edit);
	}

	
	public String getType() {
		return type;
	}

	
	public void setType(String type) {
		this.type = type;
	}

	public String getAlt() {
		return alt;
	}

	public void setAlt(String alt) {
		this.alt = alt;
	}

	
}
